package cn.hnu.qq.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Transient;

import org.apache.commons.lang.math.RandomUtils;

@Entity
public class User implements Serializable{ 

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Integer id;
	
	private String acount;
	
	private String password;
	
	private String info;
	
	private Boolean sex = true;
	
	private String ip;
	
	private Boolean state;
	
	private String session;
	
	private int headImage = RandomUtils.nextInt(7)+1;;
	
	private String nickName;
	
	private String email;
	
	private Integer userClientPort;
	
	private Set<Crowd> crowds = new HashSet<Crowd>();
	
	private Set<User> friends = new HashSet<User>();

	
	public User() {
	}

	
	/**
	 * 
	 * @param acount
	 * @param password
	 * @param info
	 * @param sex
	 * @param headImage
	 * @param nickName
	 */
	public User(String acount, String password, String info, Boolean sex,
			int headImage, String nickName,String email) {
		super();
		this.acount = acount;
		this.password = password;
		this.info = info;
		this.sex = sex;
		this.headImage = headImage;
		this.nickName = nickName;
		this.email = email;
	}



	@Id @GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(unique=true,nullable=false)
	public String getAcount() {
		return acount;
	}

	public void setAcount(String acount) {
		this.acount = acount;
	}
	
	@Column(nullable=false)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}
	
	@Column(nullable=false)
	public Boolean getSex() {
		return sex;
	}

	public void setSex(Boolean sex) {
		this.sex = sex;
	}

	@Transient
	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}
	
	@Transient
	public Boolean getState() {
		return state;
	}
	
	public void setState(Boolean state) {
		this.state = state;
	}

	@Transient	
	public String getSession() {
		return session;
	}

	public void setSession(String session) {
		this.session = session;
	}

	
	public int getHeadImage() {
		return headImage;
	}

	public void setHeadImage(int headImage) {
		this.headImage = headImage;
	}
	
	@Column(nullable=true,length=10)
	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	@ManyToMany(mappedBy="users",fetch=FetchType.EAGER,cascade = {CascadeType.MERGE})
	public Set<Crowd> getCrowds() {
		return crowds;
	}

	public void setCrowds(Set<Crowd> groups) {
		this.crowds = groups;
	}
	
	@ManyToMany (cascade = {CascadeType.MERGE,CascadeType.REFRESH,CascadeType.PERSIST},fetch=FetchType.EAGER) //产生的user_friend表没有主键
    @JoinTable(
                    name="user_friend",
                    joinColumns = {@JoinColumn(name="user_id",referencedColumnName = "id")},
                    inverseJoinColumns = {@JoinColumn(name="friend_id",referencedColumnName = "id")}
                    )
	public Set<User> getFriends() {
		return friends;
	}

	public void setFriends(Set<User> friends) {
		this.friends = friends;
	}
	
	public void addFriends(User friend){
		this.friends.add(friend);
	}

	public void joinGroup(Crowd group){
		this.crowds.add(group);
	}
	
//	@Override
//	public String toString() {
//		return "User [id=" + id + ", acount=" + acount + ", password="
//				+ password + ", info=" + info + ", sex=" + sex + ", ip=" + ip
//				+ ", state=" + state + ", session=" + session + ", headImage="
//				+ headImage + ", nickName=" + nickName + "]";
//	}

	@Transient
	public Integer getUserClientPort() {
		return userClientPort;
	}


	public void setUserClientPort(Integer userClientPort) {
		this.userClientPort = userClientPort;
	}


	@Override
	public boolean equals(Object obj) {
		if(obj == null){
			return false;
		}
		if(obj instanceof User){
			obj = (User)obj;
			if(((User) obj).acount.equals(acount)){
				return true;
			}else{
				return false;
			}
		}
		return false;
	}

	

	@Column(nullable=false,length=20)
	public String getEmail() {
		return email;
	}


	public void setEmail(String email) {
		this.email = email;
	}


	@Override
	public String toString() {
		return acount;
	}
	
	public void removeFriend(User user){
		this.friends.remove(user);
	}


	public void removeCrowd(Crowd _crowd) {
		this.crowds.remove(_crowd);	
	}


	public User findFriend(String user) {
		for(User u:friends){
			if(u.getAcount().equals(user)){
				return u;
			}
		}
		return null;
	}
	
}
